/*
	This file is part of GTimer.

    GTimer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License\.

    GTimer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with GTimer. If not, see <http://www.gnu.org/licenses/>.
	
	Copyright 2012 Muhammad Ghareeb Sideek
	Contact: MGhareeb64@gmail.com
 */

package com.gsourcecode.timer;

import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioGroup;
import android.widget.Toast;

public class SettingsActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);

		((Button) findViewById(R.id.btnSave)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Settings.get().isBeep = ((CheckBox) findViewById(R.id.chbxBeeps)).isChecked();
				Settings.get().isTTS = ((CheckBox) findViewById(R.id.chbxTTS)).isChecked();
				Settings.get().isVib = ((CheckBox) findViewById(R.id.chbxVib)).isChecked();
				Settings.get().isWakeLock = ((CheckBox) findViewById(R.id.chbxWakeLock)).isChecked();
				switch (((RadioGroup) findViewById(R.id.rbgpTest)).getCheckedRadioButtonId()) {
				case R.id.rbtn10x:
					Settings.get().skip = 10;
					break;
				case R.id.rbtn100x:
					Settings.get().skip = 100;
					break;
				default:
					Settings.get().skip = 1;
					break;
				}

				try {
					File settings = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "gtimer" + File.separator + "settings");
					ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(settings));
					out.writeObject(Settings.get());
					out.close();
					Toast.makeText(SettingsActivity.this, "Settings saved", Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					Toast.makeText(SettingsActivity.this, "Error while saving settings", Toast.LENGTH_LONG).show();
				}
				back();
			}
		});
		((Button) findViewById(R.id.btnCancel)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Toast.makeText(SettingsActivity.this, "Changes discarded", Toast.LENGTH_LONG).show();
				back();
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		runOnUiThread(new Runnable() {

			@Override
			public void run() {
				((CheckBox) findViewById(R.id.chbxBeeps)).setChecked(Settings.get().isBeep);
				((CheckBox) findViewById(R.id.chbxTTS)).setChecked(Settings.get().isTTS);
				((CheckBox) findViewById(R.id.chbxVib)).setChecked(Settings.get().isVib);
				((CheckBox) findViewById(R.id.chbxWakeLock)).setChecked(Settings.get().isWakeLock);
				switch (Settings.get().skip) {
				case 10:
					((RadioGroup) findViewById(R.id.rbgpTest)).check(R.id.rbtn10x);
					break;
				case 100:
					((RadioGroup) findViewById(R.id.rbgpTest)).check(R.id.rbtn100x);
					break;
				default:
					((RadioGroup) findViewById(R.id.rbgpTest)).check(R.id.rbtn1x);
					break;
				}
			}
		});
	}

	private void back() {
		try {
			startActivity(new Intent(SettingsActivity.this, Class.forName("com.gsourcecode.timer.GTimerMain")));
			finish();
		} catch (Exception e) {}
	}
}
